<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin', 'admin', 0);
pc_base::load_sys_class('form', '', 0);
pc_base::load_app_func('util');
pc_base::load_sys_class('format', '', 0);
class manage extends admin{
	private $db;
	public $site;
	
	function __construct() {
		parent::__construct();
		pc_base::load_app_class('global');
		pc_base::load_app_func('submenu','experience');//加载检测履历分类下是否存在子类
		$this->db = pc_base::load_model('talent_model');//加载model
		$this->admin_db = pc_base::load_model('admin_model');
		$this->experience_db = pc_base::load_model('experience_model');
		$this->experience_type_db = pc_base::load_model('experience_type_model');
		$this->talent_source_db = pc_base::load_model('talent_source_model');//加载人才关联资源模型
		$this->news = pc_base::load_model('news_nsr_model');//加载文章模型
		$this->picture = pc_base::load_model('picture_nsr_model');//加载图片模型
		$this->video = pc_base::load_model('video_nsr_model');//加载视频模型
		$this->siteid = get_siteid();//获取当前站点
		$this->type = pc_base::load_model('source_type_model');//加载资源类型模型
		$this->admin_userid = param::get_cookie('userid'); // 获取当前登录的管理员ID
		$model_setting=pc_base::load_model('site_setting_model');
		$this->author_title = $model_setting->getValue("author_title",$this->siteid);//获取作者职称
		$this->setting = $model_setting->getValue("sname_link",$this->siteid);//获取来源名称
	}
	/**
	 * 获取模特关联的资源（资讯、图片、相册、视频、赛事、机构）
	 * @date: 2017年3月20日 下午4:36:49
	 * @author: LIYANG
	 */
	public function init() {
		if (! isset($_REQUEST['talent_id']) || empty($_REQUEST['talent_id'])) {
			showmessage(L('illegal_action'), HTTP_REFERER);
		}
		$source_type=isset($_REQUEST['source_type'])?$_REQUEST['source_type']:4;
		$typeid = isset($_GET['typeid']) ? $_GET['typeid'] : '';
		
		if($source_type==1){
			$table="nsrweb_news_nsr";
			$btn_info="资讯";
		}elseif($source_type==2){
			$table="nsrweb_video_nsr";
			$btn_info="视频";
		}elseif($source_type==3){
			$table="nsrweb_picture_nsr";
			$btn_info="图集";
		}
		$select_categorys = $this->type->getSource($source_type,$typeid);
		$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
		$id = isset($_GET['talent_id']) ? $_GET['talent_id'] : showmessage(L('message not exits'),HTTP_REFERER);
		$moter_info = $this->db->get_one(array('id'=>$id));
		$sql="SELECT n.*,c.id as cid,c.use_type,c.is_top,c.sort,c.updated_by as supdated_by  FROM nsrweb_talent_source AS c LEFT JOIN ".$table." AS n ON c.source_id = n.id WHERE n.`status` = 1";
		
		// 搜索框
		$title = isset($_GET['title']) ? $_GET['title'] : '';
		
		if (isset($_GET['search'])) {
			if ($title) {
				$sql .= " and n.`title` LIKE '%$title%'";
			}
			if ($typeid) {
				$sql .= " and n.type_id = {$typeid}";
			}
		}
		$admin_model = pc_base::load_model('admin_model');
		$adminInfo = $admin_model->getRealnameInfo();
		$sql.=" AND c.talent_id=".$id." AND c.source_type = ".$source_type." AND n.siteid = ".$this->siteid." ORDER BY c.is_top DESC,c.sort DESC";
		$sourceinfo = $this->talent_source_db->my_listinfo($sql, $page, 10);
		$sourcepages = $this->talent_source_db->pages;
		$show_header = false;
		pc_base::load_sys_class('form', '', 0);
		include $this->admin_tpl("resource_asso");
	}
	
	public function moter_record() {
		if (! isset($_REQUEST['talent_id']) || empty($_REQUEST['talent_id'])) {
			showmessage(L('illegal_action'), HTTP_REFERER);
		}
		$source_type=isset($_REQUEST['source_type'])?$_REQUEST['source_type']:4;
		$talent_type = isset($_REQUEST['talent_type']) ? $_REQUEST['talent_type'] : 1;
		if ($source_type == 4){
			$btn_info="履历";
		}
		$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
		$id = isset($_GET['talent_id']) ? $_GET['talent_id'] : showmessage(L('message not exits'),HTTP_REFERER);
		/**
		 * 是否隐藏添加履历按钮
		 */
		$condition = "";
		$condition .= "siteid = ".$this->siteid." and status =1";
		$experience_type = $this->experience_type_db->listinfo($condition,'id ASC');
		$is_experience = $this->experience_db->select(array('talent_id'=>$_GET['talent_id'],'siteid'=>$this->siteid,'status'=>1),'experience_id');
		$is_experience_arr = array();
		foreach ($is_experience as $k=>$v) {
			$is_experience_arr[$k] = $v['experience_id'];
		}
		$experience_arr = array();
		foreach ($experience_type as $k=>$v) {
			if (in_array($v['id'], $is_experience_arr)) {
				unset($experience_type[$k]);
			}
			$experience_arr = check_submenu($v['id']);
			if ($experience_arr == true) {
				unset($experience_type[$k]);
			}
		}
		if (empty($experience_type)) {
			$experience_btn = true;
		}
		/**
		 * 是否隐藏添加履历按钮结束
		 */
		$where = "";
		$where .= "talent_id = '$id' and status = 1 and siteid = '$this->siteid'";
		$moter_info = $this->db->get_one(array('id'=>$id));
		if (isset($_GET['dosubmit'])) {
			$keyword = $_GET['experience_type'];
			$sql = "";
			$sql .= "select a.*,b.name,en_name from nsrweb_experience as a left join nsrweb_experience_type as b on a.experience_id = b.id where b.name like '%$keyword%' and a.talent_id = ".$_GET['talent_id']." and a.status = 1";
			$experience_info = $this->experience_db->my_listinfo($sql);//获取履历列表
		} else {
			$experience_info = $this->experience_db->listinfo($where,'id DESC',$page,10);//获取履历列表
		}
		$admin_model = pc_base::load_model('admin_model');
		$adminInfo = $admin_model->getRealnameInfo();
		$new_arr1 = array();
		foreach ($experience_info as $k=>$v) {
			$new_arr1 = $this->experience_type_db->get_one(array('id'=>$v['experience_id']),'name');
			$experience_info[$k]['username'] = $adminInfo[$v['updated_by']];
			$experience_info[$k]['experience_name'] = $new_arr1['name'];
		}
		foreach ($talent_info as $k=>$v){
			//获取用户管理员真实名称数据
			$talent_info[$k]['username'] = $adminInfo[$v['updated_by']];
		}
		$pages = $this->db->pages;
		$show_header = false;
		pc_base::load_sys_class('form', '', 0);
		include $this->admin_tpl("experience_list","experience");
	}
	
	/**
	* 查看资源详情
	* @date: 2017年3月29日 下午1:58:38
	* @author: LIYANG
	*/
	public function detail() {
		$id = isset($_GET['id']) ? $_GET['id'] : showmessage(L('message not exits'),HTTP_REFERER);//资源ID
		$talent_id = isset($_GET['talent_id']) ? $_GET['talent_id'] : showmessage(L('message not exits'),HTTP_REFERER);//人才ID
		$info = $this->experience_db->get_one(array('id'=>$id,'talent_id'=>$talent_id,'status'=>1));
		$experience_name = $this->experience_type_db->get_one(array('id'=>$info['experience_id']));//履历类型名称
		$info['experience_name'] = $experience_name['name'];
		$show_header = false;
		pc_base::load_sys_class('form', '', 0);
		include $this->admin_tpl('resource_detail');
		
	}
	
	/**
	 * 添加
	 */
	public function add()
	{
	
		if (isset($_POST['dosubmit'])) {
	
			$this->news = pc_base::load_model('news_nsr_model');
			$this->picture = pc_base::load_model('picture_nsr_model');
			$this->video = pc_base::load_model('video_nsr_model');
			$source_type=isset($_REQUEST['talent_source_type'])?$_REQUEST['talent_source_type']:1;
			if($source_type==1){
				$model=$this->news;
				$information =$model->check($_POST['information']);
			}elseif($source_type==2){
				$model=$this->video;
				$information =$model->check($_POST['manage']);
			}elseif($source_type==3){
				$model=$this->picture;
				$information =$model->check($_POST['picture']);
			}
	
	
	
			$id = $model->edit($information);
			//添加缓存
// 			$this->news->redisCache($id);
			if ($id) {
				$data=array();
				$data['talent_id']=$_POST['talent_id'];
				$data['source_id']=$id;
				$data['source_type']=$source_type;
				$data['is_top']=0;
				$data['sort']=0;
				$data['use_type']=1;#使用类型:1-后台录入;2-app上传;3-app模特卡 updated by 吴发顶；advised by 刘世杰；time：2017-11-14 21:29:36
				$data['created_by'] =  $_SESSION['userid'];
				$data['updated_by'] =  $_SESSION['userid'];
	
				$data['create_time'] = date("Y-m-d H:i:s",time());
				$data['update_time'] = date("Y-m-d H:i:s",time());
				$res = $this->talent_source_db->insert($data);
				if ($res) {
					//记录行为日志
					if ($source_type==1) {
						$log_content = '<span style="color:green;">添加资讯</span>[新生成资讯id：'.$id.'；资讯标题：'.$information['title'].']';
					} elseif($source_type==2) {
						$log_content = '<span style="color:green;">添加视频</span>[新生成视频id：'.$id.'；视频标题：'.$information['title'].']';
					} elseif ($source_type==3){
						$log_content = '<span style="color:green;">添加图集</span>[新生成图集id：'.$id.'；图集标题：'.$information['title'].']';
					}
					$this->manage_admin_log($log_content);
// 					showmessage(L('add_success'), '?m=resource_asso&c=manage&a=init&talent_id='.$data['talent_id'],'','add');
					showmessage(L('add_success').L('2s_close'),'blank','','','function set_time() {$("#secondid").html(1);}setTimeout("set_time()", 500);setTimeout("window.close()", 1200);');
				} else {
					showmessage(L('add_error'), HTTP_REFERER);
				}
			} else {
				showmessage(L('add_information_error'), HTTP_REFERER);
			}
		} else {
			$show_dialog = $show_validator = true;
			$show_header = '';
			$source_type=isset($_REQUEST['source_type'])?$_REQUEST['source_type']:1;
			$types = $this->type->getSource($source_type);
	
			if (! isset($_REQUEST['talent_id']) || empty($_REQUEST['talent_id'])) {
				showmessage(L('illegal_action'), HTTP_REFERER);
			}
			$talent_id= $_REQUEST['talent_id'];
			pc_base::load_sys_class('format', '', 0);
			pc_base::load_sys_class('form', '', 0);
	
			$action_source="talentant";
	
			if($source_type==1){
				include $this->admin_tpl('information_add','information');
			}elseif($source_type==2){
				include $this->admin_tpl('manage_add','video');
			}elseif($source_type==3){
				include $this->admin_tpl('picture_add','picture');
			}
	
		}
	}
	
	public function bindView(){
		$talent_id= $_REQUEST['talent_id'];
		$source_type=isset($_REQUEST['source_type'])?$_REQUEST['source_type']:1;
		$source_array=$this->talent_source_db->select(array("talent_id"=>$talent_id,"source_type"=>$source_type),"source_id");
		$typeid = isset($_GET['typeid']) ? $_GET['typeid'] : '';
		$select_categorys = $this->type->getSource($source_type,$typeid);
		$where=" status=1 and  siteid =" . $this->siteid;
		if(!empty($source_array)){
			foreach ($source_array as $k => $v) {
				$id[] = intval($v['source_id']);
			}
			$id = implode('\',\'', $id);
			$where.=" and id not in ('$id')";
		}
	
		// 搜索框
		$keywords = isset($_GET['keywords']) ? $_GET['keywords'] : '';
	
		if (isset($_GET['search'])) {
			if ($keywords) {
				$where .= " and `title` LIKE '%$keywords%'";
			}
			if ($typeid) {
				$where.= " and type_id = {$typeid}";
			}
		}

		//获取用户管理员真实姓名数据
		$admin_model = pc_base::load_model('admin_model');
		$adminInfo = $admin_model->getRealnameInfo();
	
		$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
		if($source_type==1){
			$model=$this->news;
	
		}elseif($source_type==2){
			$model=$this->video;
	
		}elseif($source_type==3){
			$model=$this->picture;
	
		}
		$url = "?m=resource_asso&c=manage&a=bindTypeSource&talent_id=".$talent_id;
		$infos=$model->listinfo($where,'id DESC',$page,10);
		$pages = $model->pages;
		if($source_type==1){
			include $this->admin_tpl('information_select',"information");
		}elseif($source_type==2){
			include $this->admin_tpl('video_select','video');
		}elseif($source_type==3){
			include $this->admin_tpl('picture_select','picture');
		}
	
	
	
	}
	
	
	public function  bindAction(){
		$talent_id= $_REQUEST['talent_id'];
		$relation=$_REQUEST['relation'];
		$source=explode("|",$relation);
		$data=array();
		$source_type=isset($_REQUEST['source_type'])?$_REQUEST['source_type']:1;
	
	
		foreach($source as $k=>$v){
	
			$data[$k]['talent_id']=$talent_id;
			$data[$k]['source_id']=$v;
			$data[$k]['source_type']=$source_type;
			$data[$k]['is_top']=0;
			$data[$k]['sort']=0;
			$data[$k]['use_type']=1;#使用类型:1-后台录入;2-app上传;3-app模特卡 updated by 吴发顶；advised by 刘世杰；time：2017-11-14 21:28:14
			$data[$k]['created_by'] =  $_SESSION['userid'];
			$data[$k]['updated_by'] =  $_SESSION['userid'];
	
			$data[$k]['create_time'] = date("Y-m-d H:i:s");
			$data[$k]['update_time'] = date("Y-m-d H:i:s");
		}
		$res=$this->talent_source_db->multiInsert($data);
		if($res){
			echo  json_encode(array("status"=>true,"code"=>200,"msg"=>"选择成功"));
		}else{
			echo  json_encode(array("status"=>false,"code"=>1000,"msg"=>"选择失败"));
		}
	}
	
	/**
	 * 关联资源一键导入
	 */
	public function bindTypeSource(){
		$talent_id = (int) $_REQUEST['talent_id'];
		$source_type=isset($_REQUEST['source_type'])?(int)$_REQUEST['source_type']:1;
		$type_id = (int) $_REQUEST['type_id'];
		$source_array=$this->course_source->select(array("talent_id"=>$talent_id,"source_type"=>$source_type),"source_id");
		$typeInfo = $this->type->get_one(array('id'=>$type_id));
		$where = "";
		$where .= "type_id = ".$type_id;
		if(!empty($source_array)){
			foreach ($source_array as $k => $v) {
				$id[] = intval($v['source_id']);
			}
			$id = implode('\',\'', $id);
			$where.=" and id not in ('$id')";
		}
		if ($typeInfo['parentid'] > 0) {
			if($source_type==1){
				$model=$this->news;
			}elseif($source_type==2){
				$model=$this->video;
			}elseif($source_type==3){
				$model=$this->picture;
			}
			$info = $model->select($where,'id');
			$data=array();
			foreach($info as $k=>$v){
				
				$data[$k]['talent_id']=$talent_id;
				$data[$k]['source_id']=$v['id'];
				$data[$k]['source_type']=$source_type;
				$data[$k]['created_by'] =  $_SESSION['userid'];
				$data[$k]['updated_by'] =  $_SESSION['userid'];
				
				$data[$k]['create_time'] = date("Y-m-d H:i:s",time());
				$data[$k]['update_time'] = date("Y-m-d H:i:s",time());
			}
			if (empty($data)) {
				echo  json_encode(array("status"=>true,"code"=>200,"msg"=>"一键导入成功"));
				exit();
			}
			$res=$this->talent_source_db->multiInsert($data);
			if($res){
				echo  json_encode(array("status"=>true,"code"=>200,"msg"=>"一键导入成功"));
			}else{
				echo  json_encode(array("status"=>false,"code"=>1000,"msg"=>"一键导入失败"));
			}
		} else {
			echo  json_encode(array("status"=>false,"code"=>1000,"msg"=>"请选择最底层类型"));
		}
	}
	
	public function  top(){
		$result = $this->talent_source_db->update(array('is_top'=> $_GET['is_top']), array(
				'id' => $_GET['id']
		));
		if($result){
			echo  json_encode(array("status"=>true,"code"=>200,"msg"=>"更新成功"));
		}else{
			echo  json_encode(array("status"=>false,"code"=>1000,"msg"=>"更新失败"));
		}
	}
	
	/*
	 * ajax 改变 用户资源关联表(nsrweb_talent_source)中的 use_type(使用类型:1-后台录入;2-app上传;)字段
	 * 2017年11月20日14:33:58
	 * angel_in_us
	 */
	public function  use_type(){
	    $id = intval($_GET['id']);
	    if(!$id){
	        return json_encode(array("status"=>false,"code"=>1001,"msg"=>"非法请求"));
	    }
		$result = $this->talent_source_db->update(array('use_type'=> $_GET['use_type']), array(
				'id' => $id
		));
		if($result){
			//记录行为日志
			$log_content = '<span style="color:blue;">改变资源use_type</span>[nsrweb_talent_source表id：'.$id.'；use_type：'.$_GET['use_type'].']';
			$this->manage_admin_log($log_content);
			echo  json_encode(array("status"=>true,"code"=>200,"msg"=>"更新成功"));
		}else{
			echo  json_encode(array("status"=>false,"code"=>1000,"msg"=>"更新失败"));
		}
	}
	
	/**
	 * 删除、批量删除
	 */
	public function del()
	{
		if (! isset($_REQUEST['id']) || empty($_REQUEST['id'])) {
			showmessage(L('illegal_action'), HTTP_REFERER);
		}
		$id = $_REQUEST['id'];
		if (is_array($id)) {
			foreach ($id as $k => $v) {
				if (intval($v)) {
					$id[$k] = intval($v);
				} else {
					unset($id[$k]);
				}
			}
			$id = implode('\',\'', $id);
		}
		$result = $this->talent_source_db->delete("id in ('$id')");
		
		
		if ($result) {
			showmessage(L('del_success'), HTTP_REFERER);
		} else {
			showmessage(L('del_error'), HTTP_REFERER);
		}
	}
	
	/**
	 * 资讯批量删除
	 */
	public function deleteAll ()
	{
	
		if(isset($_GET['dosubmit'])) {
			$id = implode('\',\'', $_POST['ids']);
	
			$result = $this->talent_source_db->delete("id in ('$id')");
			if ($result) {
				showmessage(L('del_success'), HTTP_REFERER);
			} else {
				showmessage(L('del_error'), HTTP_REFERER);
			}
		} else {
			showmessage(L('operation_failure'));
		}
	}
	
	/**
	 * 排序
	 */
	public function listorder() {
		if(isset($_GET['dosubmit'])) {
			foreach($_POST['listorders'] as $id => $listorder) {
	
				$result = $this->talent_source_db->update(array('sort'=>$listorder), array(
						'id' => $id
				));
	
			}
			showmessage(L('operation_success'), HTTP_REFERER);
		} else {
			showmessage(L('operation_failure'), HTTP_REFERER);
		}
	}
	
	/**
	 * 资源编辑
	 */
	public function resource_edit(){
		if ($_REQUEST['source_type'] == 1) {//资讯
			$category_source_model = pc_base::load_model('category_source_model');
			if (!isset($_GET['rid']) || empty($_GET['rid'])) {
				showmessage(L('illegal_action'), HTTP_REFERER);
			}
			$id = intval($_GET['rid']);
			if (isset($_POST['dosubmit']) && !empty($_POST['dosubmit'])) {
				$status = trim($_REQUEST['status']);
				$information = $this->news->check($_POST['information'], 'edit');
				$titlepd = $this->public_check_picture($_POST['information']['title'],$_POST['source_type']);
				if (!$titlepd) {
					showmessage(L('information_exist'), HTTP_REFERER);
				}
				$ids = $this->news->edit($information, $id);
				if ($ids) {
					$contest_source_model = pc_base::load_model('contest_source_model');
					$contest_source_model->delete(array('source_id'=>$id,'source_type'=>1));
					if ($_POST['contestid']) {
						$contest_source_model->edit($id, $_POST['contestid'], 1);
					}
					//如果页面端清空已绑定的栏目，则$_POST['othor_catid']为空，删除所有已绑定的栏目
					$data = array();
					if($_POST['othor_catid']){
						$cateList = array_unique($_POST['othor_catid']);
						foreach ($cateList as $k => $v) {
							//先删除已绑定的栏目资讯，以最新提交的为准，吴发顶，2017-08-02 15:25:28
							$del_flag = $category_source_model->delete(array('source_type'=>1,'source_id'=>$id));
							$data[$k]['cate_id']=$v;
							$data[$k]['source_id']=$id;
							$data[$k]['source_type']=1;
							$data[$k]['created_by'] =  $_SESSION['userid'];
							$data[$k]['updated_by'] =  $_SESSION['userid'];
							
							$data[$k]['create_time'] = date("Y-m-d H:i:s",time());
							$data[$k]['update_time'] = date("Y-m-d H:i:s",time());
						}
						if(!empty($data)){
							$res=$category_source_model->multiInsert($data);
						}
					}else{
						$del_flag = $category_source_model->delete(array('source_type'=>1,'source_id'=>$id));
					}
					
					//记录行为日志
					$log_content = '<span style="color:blue;">修改资讯</span>[资讯id：'.$id.'；资讯标题：'.$information['title'].']';
					$this->manage_admin_log($log_content);
					if($status=='unchecked'){
						showmessage(L('edit_information_success'), '?m=resource_asso&c=manage&a=init&source_type='.$_POST['source_type'].'&talent_id='.$_POST['talent_id'].'&talent_type='.$_POST['talent_type'].'&menuid='.$_POST['menuid']);
					}else{
						showmessage(L('edit_information_success'), '?m=resource_asso&c=manage&a=init&source_type='.$_POST['source_type'].'&talent_id='.$_POST['talent_id'].'&talent_type='.$_POST['talent_type'].'&menuid='.$_POST['menuid']);
					}
				} else {
					showmessage(L('edit_information_error'), HTTP_REFERER);
				}
			} else {
				$info = $this->news->get_one(array('id'=>$id, 'siteid'=>$this->siteid));
				extract($info);
				$status = trim($_REQUEST['status']);
				$parentid = $type_id?$type_id:param::get_cookie('catid_1');
				//取资讯类型
				$types = $this->type->getSource(1,$parentid);
				$contest_source_model = pc_base::load_model('contest_source_model');
				$contest_model = pc_base::load_model('contest_model');
				$csInfo = $contest_source_model->get_one(array('source_id'=>$id));
				$contest_id = $csInfo['contest_id']?$csInfo['contest_id']:param::get_cookie('contest_id_1');
				//取正在进行赛事
				$categorys = $contest_model->now_contest_select($contest_id);
				//取绑定栏目信息
				$category_model = pc_base::load_model('category_model');
				$cateList = $category_source_model->select(array('source_id'=>$id,'source_type'=>1),'cate_id');
				$cates = '';
				foreach ($cateList as $k=>$v) {
					$cateInfo = $category_model->get_one(array('catid'=>$v['cate_id']),'catid,catname');
					$cates .= '<li id="v'.$cateInfo['catid'].'">·<input type="hidden" name="othor_catid[]" value="'.$cateInfo['catid'].'"><span>'.$cateInfo['catname'].'</span><a href="javascript:;" class="close" onclick="remove_id('."'v".$cateInfo['catid']."'".')"></a></li>';
				}
				//获取站点模板信息
				pc_base::load_app_func('global', 'admin');
				$show_header = false;
				include $this->admin_tpl('news_edit');
			}
		} elseif ($_REQUEST['source_type'] == 2){//视频
			$category_source_model = pc_base::load_model('category_source_model');
			if (! isset($_GET['rid']) || empty($_GET['rid'])) {
				showmessage(L('illegal_action'), HTTP_REFERER);
			}
			$id = intval($_GET['rid']);
			if (isset($_POST['dosubmit']) && ! empty($_POST['dosubmit'])) {
				$titlepd = $this->public_check_picture($_POST['manage']['title'],$_POST['source_type']);
				if (!$titlepd) {
					showmessage(L('title-exist'), HTTP_REFERER);
				}
				$manage = $this->video->check($_POST['manage'], 'edit');
				$ids = $this->video->edit($manage, $id);
				if ($ids) {
					$contest_source_model = pc_base::load_model('contest_source_model');
					$contest_source_model->delete(array('source_id'=>$id,'source_type'=>2));
					if ($_POST['contestid']) {
						$contest_source_model->edit($id, $_POST['contestid'], 2);
					}
					//如果页面端清空已绑定的栏目，则$_POST['othor_catid']为空，删除所有已绑定的栏目
					$data = array();
					if($_POST['othor_catid']){
						$cateList = array_unique($_POST['othor_catid']);
						foreach ($cateList as $k => $v) {
							//先删除已绑定的栏目资讯，以最新提交的为准，吴发顶，2017-08-02 15:25:28
							$del_flag = $category_source_model->delete(array('source_type'=>2,'source_id'=>$id));
							$data[$k]['cate_id']=$v;
							$data[$k]['source_id']=$id;
							$data[$k]['source_type']=2;
							$data[$k]['created_by'] =  $_SESSION['userid'];
							$data[$k]['updated_by'] =  $_SESSION['userid'];
							
							$data[$k]['create_time'] = date("Y-m-d H:i:s",time());
							$data[$k]['update_time'] = date("Y-m-d H:i:s",time());
						}
						if(!empty($data)){
							$res=$category_source_model->multiInsert($data);
						}
					}else{
						$del_flag = $category_source_model->delete(array('source_type'=>2,'source_id'=>$id));
					}
					
					//记录行为日志
					$log_content = '<span style="color:blue;">修改视频</span>[视频id：'.$id.'；视频标题：'.$manage['title'].']';
					$this->manage_admin_log($log_content);
					
					showmessage(L('operation_success'), '?m=resource_asso&c=manage&a=init&source_type='.$_POST['source_type'].'&talent_id='.$_POST['talent_id'].'&talent_type='.$_POST['talent_type'].'&menuid='.$_POST['menuid']);
				} else {
					showmessage(L('operation_failure'), HTTP_REFERER);
				}
			} else {
				pc_base::load_sys_class('form', '', 0);
				$show_dialog = $show_validator = true;
				$info = $this->video->get_one(array('id'=>$id, 'siteid'=>$this->siteid));
				extract($info);
				$parentid = $type_id?$type_id:param::get_cookie('catid_2');
				//取视频类型
				$types = $this->type->getSource(2,$parentid);
				$contest_source_model = pc_base::load_model('contest_source_model');
				$contest_model = pc_base::load_model('contest_model');
				$csInfo = $contest_source_model->get_one(array('source_id'=>$id));
				$contest_id = $csInfo['contest_id']?$csInfo['contest_id']:param::get_cookie('contest_id_2');
				//取正在进行赛事
				$categorys = $contest_model->now_contest_select($contest_id);
				//取绑定栏目信息
				$category_model = pc_base::load_model('category_model');
				$cateList = $category_source_model->select(array('source_id'=>$id,'source_type'=>2),'cate_id');
				$cates = '';
				foreach ($cateList as $k=>$v) {
					$cateInfo = $category_model->get_one(array('catid'=>$v['cate_id']),'catid,catname');
					$cates .= '<li id="v'.$cateInfo['catid'].'">·<input type="hidden" name="othor_catid[]" value="'.$cateInfo['catid'].'"><span>'.$cateInfo['catname'].'</span><a href="javascript:;" class="close" onclick="remove_id('."'v".$cateInfo['catid']."'".')"></a></li>';
				}
				
				//获取站点模板信息
				$show_header = false;
				pc_base::load_app_func('global', 'admin');
				include $this->admin_tpl('video_nsr_edit');
			}
		} elseif ($_REQUEST['source_type'] == 3){//图片
			$category_source_model = pc_base::load_model('category_source_model');
			if (! isset($_GET['rid']) || empty($_GET['rid'])) {
				showmessage(L('illegal_action'), HTTP_REFERER);
			}
			$id = intval($_GET['rid']);
			if (isset($_POST['dosubmit']) && ! empty($_POST['dosubmit'])) {
				$titlepd = $this->public_check_picture($_POST['picture']['title'],$_POST['source_type']);
				if (!$titlepd) {
					showmessage(L('title-exist'), HTTP_REFERER);
				}
				$picture = $this->picture->check($_POST['picture'], 'edit');
				$ids = $this->picture->edit($picture, $id);
				if ($ids) {
					$contest_source_model = pc_base::load_model('contest_source_model');
					$contest_source_model->delete(array('source_id'=>$id,'source_type'=>3));
					if ($_POST['contestid']) {
						$contest_source_model->edit($id, $_POST['contestid'], 3);
					}
					//如果页面端清空已绑定的栏目，则$_POST['othor_catid']为空，删除所有已绑定的栏目
					$data = array();
					if($_POST['othor_catid']){
						$cateList = array_unique($_POST['othor_catid']);
						foreach ($cateList as $k => $v) {
							//先删除已绑定的栏目资讯，以最新提交的为准，吴发顶，2017-08-02 15:25:28
							$del_flag = $category_source_model->delete(array('source_type'=>3,'source_id'=>$id));
							$data[$k]['cate_id']=$v;
							$data[$k]['source_id']=$id;
							$data[$k]['source_type']=3;
							$data[$k]['created_by'] =  $_SESSION['userid'];
							$data[$k]['updated_by'] =  $_SESSION['userid'];
							
							$data[$k]['create_time'] = date("Y-m-d H:i:s",time());
							$data[$k]['update_time'] = date("Y-m-d H:i:s",time());
						}
						if(!empty($data)){
							$res=$category_source_model->multiInsert($data);
						}
					}else{
						$del_flag = $category_source_model->delete(array('source_type'=>3,'source_id'=>$id));
					}
					
					//记录行为日志
					$log_content = '<span style="color:blue;">修改图集</span>[图集id：'.$id.'；图集标题：'.$picture['title'].']';
					$this->manage_admin_log($log_content);
					
					showmessage(L('edit_picture_success'), '?m=resource_asso&c=manage&a=init&source_type='.$_POST['source_type'].'&talent_id='.$_POST['talent_id'].'&talent_type='.$_POST['talent_type'].'&menuid='.$_POST['menuid']);
				} else {
					showmessage(L('edit_picture_error'), HTTP_REFERER);
				}
			} else {
				$show_dialog = $show_validator = true;
				$info = $this->picture->get_one(array('id'=>$id, 'siteid'=>$this->siteid));
				extract($info);
				//组图反序列化
				$pictureurls = json_decode($pictureurls);//var_dump($pictureurls);die;
				$parentid = $type_id?$type_id:param::get_cookie('catid_3');
				//取图集类型
				$types = $this->type->getSource(3, $parentid);
				$contest_source_model = pc_base::load_model('contest_source_model');
				$contest_model = pc_base::load_model('contest_model');
				$csInfo = $contest_source_model->get_one(array('source_id'=>$id));
				$contest_id = $csInfo['contest_id']?$csInfo['contest_id']:param::get_cookie('contest_id_3');
				//取正在进行赛事
				$categorys = $contest_model->now_contest_select($contest_id);
				//取绑定栏目信息
				$category_model = pc_base::load_model('category_model');
				$cateList = $category_source_model->select(array('source_id'=>$id,'source_type'=>3),'cate_id');
				$cates = '';
				foreach ($cateList as $k=>$v) {
					$cateInfo = $category_model->get_one(array('catid'=>$v['cate_id']),'catid,catname');
					$cates .= '<li id="v'.$cateInfo['catid'].'">·<input type="hidden" name="othor_catid[]" value="'.$cateInfo['catid'].'"><span>'.$cateInfo['catname'].'</span><a href="javascript:;" class="close" onclick="remove_id('."'v".$cateInfo['catid']."'".')"></a></li>';
				}
				
				//获取站点模板信息
				$show_header = false;
				pc_base::load_app_func('global', 'admin');
				include $this->admin_tpl('picture_edit');
			}
		}
	}
	
	/**
	 * 检验标题否存在，避免标题重复
	 */
	public function public_check_picture ($title,$source_type)
	{
		if ($source_type == 1) {
			$model = pc_base::load_model('news_nsr_model');//加载文章模型
		} elseif ($source_type == 2) {
			$model = pc_base::load_model('video_nsr_model');//加载视频模型
		} elseif ($source_type == 3) {
			$model = pc_base::load_model('picture_nsr_model');//加载图片模型
		}
		if(!$title) showmessage(L('no-title'), HTTP_REFERER);
		if(pc_base::load_config('system', 'charset')=='gbk') {
			$title = safe_replace(iconv('UTF-8', 'GBK', $title));
		}
		$title = addslashes($title);
		if($_GET['rid']) {
			$id = intval($_GET['rid']);
			$r = $model->get_one(array('id'=>$id, 'siteid' => $this->siteid));
			if($r['title'] == $title) {
				return true;
			}
		}
		$r = $model->get_one(array('siteid' => $this->siteid, 'title' => $title, 'status'=>1), 'id');
		if($r['id']) {
			return false;
		} else {
			return true;
		}
	}
			
}